﻿Public Class SettingsForm
    Dim setMediaPlayer, setCountry, setUser, setPass As String
    Dim setStatFormat As Boolean
    Dim setSirenInfo As Boolean = False

    Private Sub SettingsForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ''Check for Windows Media Player
        'RadioButtonWMP.Enabled = File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) & My.Settings.path2wmp)
        ''Check for VLC Media Player
        'RadioButtonVLC.Enabled = File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) & My.Settings.path2vlc)
        ''Set defaults if none are already set
        ''Check the settings for previous selection
        'If My.Settings.sinetMediaPlayer.EndsWith("wmplayer.exe") Then
        'RadioButtonWMP.Checked = True
        'ElseIf My.Settings.sinetMediaPlayer.EndsWith("vlc.exe") Then
        'RadioButtonVLC.Checked = True
        'ElseIf My.Settings.sinetMediaPlayer.Length > 1 Then
        'The "other" option
        'TextBoxOther.Enabled = True
        'ButtonBrowseOtherFile.Enabled = True
        'RadioButtonOther.Checked = True
        'TextBoxOther.Text = My.Settings.sinetMediaPlayer
        'If Not File.Exists(My.Settings.sinetMediaPlayer) Then
        'GroupBoxPlayer.BackColor = Color.Red
        'End If
        'End If
        Try
            ''Set the country radio button
            If My.Settings.sinetCountry.Length > 1 Then
                If My.Settings.sinetCountry = "us" Then
                    RadioButtonUS.Checked = True
                ElseIf My.Settings.sinetCountry = "ca" Then
                    RadioButtonCA.Checked = True
                End If
            End If
            'Set the password box
            If My.Settings.sinetPass.Length > 0 Then
                TextBoxPassword.PasswordChar = Nothing
                TextBoxPassword.Text = "<ENCRYPTED>"
            End If
            ''Set the pop-up notification time
            If Not My.Settings.sinetNotification = 0 Then
                CheckBoxNoteWindow.Checked = True
                ComboBoxPopupSec.Visible = True
                ComboBoxPopupSec.SelectedIndex = My.Settings.sinetNotification
                LabelSecInfo.Visible = True
            End If
            ''Set the guest flag
            If My.Settings.sinetGuest <> Nothing Then
                CheckBoxGuest.Checked = My.Settings.sinetGuest
            Else
                CheckBoxGuest.Checked = False
            End If
            'Set the rest of the forms default checks
            CheckBoxPidgin.Checked = My.Settings.sinetPidginStatus
            CheckBoxStoreSongInfo.Checked = My.Settings.sinetLogSong
            CheckBoxTaskBar.Checked = My.Settings.sinetTaskBarOn
            CheckBoxTaskTray.Checked = My.Settings.sinetTaskTrayOn
            CheckBoxSortByName.Checked = My.Settings.sinetSortByName
            ''remember the sort order for post-save function
            setStatFormat = My.Settings.sinetSortByName
            TextBoxUsername.Text = My.Settings.sinetUser
            CheckBoxBitRate.Checked = My.Settings.sinetHighBitrate
            setCountry = My.Settings.sinetCountry
            ''SiNet Siren settings
            TextBoxSirenUser.Text = My.Settings.sinetSirenUser
            TextBoxSirenPass.Text = My.Settings.sinetSirenPass
            CheckBoxSirenConnect.Checked = My.Settings.sinetSirenOnConnect
            ''Setup the proxy settings
            TextBoxProxyServer.Text = My.Settings.proxyServer
            TextBoxProxyUser.Text = My.Settings.proxyUser
            If My.Settings.proxyUser.Length > 0 Then
                TextBoxProxyPass.PasswordChar = Nothing
                TextBoxProxyPass.Text = "<ENCRYPTED>"
            End If
            
        Catch ex As Exception
            writeLog("2", "Error loading settings: " & ex.Message, True)
            Dim deadSettings As DialogResult = MessageBox.Show("Your SiNet settings file is corrupt, we're going to build a new one. Please re-enter your settings.", "- SiNet Fatal Error -", MessageBoxButtons.OK, MessageBoxIcon.Error)
            If deadSettings = Windows.Forms.DialogResult.OK Then
                My.Settings.Reset()
                Me.Dispose()
            End If
        End Try
    End Sub

    Private Sub ButtonCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCancel.Click
        Main.NotifyIconMain.Visible = My.Settings.sinetTaskTrayOn
        Main.ShowInTaskbar = My.Settings.sinetTaskBarOn
        Me.Close()
    End Sub

    Private Sub RadioButtonWMP_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonWMP.CheckedChanged
        ''Not implimented yet
        'TextBoxOther.Enabled = False
        'ButtonBrowseOtherFile.Enabled = False
        'setMediaPlayer = (Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) & My.Settings.path2wmp).ToLower
    End Sub

    Private Sub RadioButtonVLC_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonVLC.CheckedChanged
        ''Not implimented yet
        'TextBoxOther.Enabled = False
        'ButtonBrowseOtherFile.Enabled = False
        'setMediaPlayer = (Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) & My.Settings.path2vlc).ToLower
    End Sub

    Private Sub ButtonBrowseOtherFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBrowseOtherFile.Click
        ''Not implimented yet
        'If OpenFileDialogOther.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
        'TextBoxOther.Text = OpenFileDialogOther.FileName
        'TextBoxOther.BackColor = Color.Empty
        'End If
    End Sub

    Private Sub RadioButtonOther_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonOther.CheckedChanged
        ''Not implimented yet
        'TextBoxOther.Enabled = True
        'ButtonBrowseOtherFile.Enabled = True
    End Sub

    Private Sub CheckBoxTaskTray_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBoxTaskTray.CheckedChanged
        If CheckBoxTaskTray.Checked Then
            Main.NotifyIconMain.Visible = True
        Else
            Main.NotifyIconMain.Visible = False
        End If
    End Sub

    Private Sub CheckBoxTaskBar_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBoxTaskBar.CheckedChanged
        If CheckBoxTaskBar.Checked Then
            Main.ShowInTaskbar = True
            Me.BringToFront()
        Else
            Main.ShowInTaskbar = False
            Me.BringToFront()
        End If
    End Sub

    Private Sub CheckBoxStoreSongInfo_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBoxStoreSongInfo.CheckedChanged
        If CheckBoxStoreSongInfo.Checked Then
            Main.ShowSongArchiveToolStripMenuItem.Enabled = True
            Me.BringToFront()
        Else
            Main.ShowSongArchiveToolStripMenuItem.Enabled = False
            Me.BringToFront()
        End If
    End Sub

    Private Sub RadioButtonUS_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonUS.CheckedChanged
        setCountry = "us"
    End Sub

    Private Sub RadioButtonCA_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonCA.CheckedChanged
        setCountry = "ca"
    End Sub

    Private Sub TextBoxPassword_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBoxPassword.GotFocus, TextBoxProxyPass.GotFocus
        TextBoxPassword.Text = Nothing
        TextBoxPassword.PasswordChar = "*"
    End Sub

    Private Sub TextBoxPassword_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBoxPassword.LostFocus, TextBoxProxyPass.LostFocus
        If TextBoxPassword.TextLength = 0 Then
            TextBoxPassword.PasswordChar = Nothing
            TextBoxPassword.Text = "<ENCRYPTED>"
        End If
    End Sub

    Private Sub ButtonStore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStore.Click
        Dim bye As Integer = 0
        Try
            ''Disabling this option for the first release, maybe later.
            'If RadioButtonOther.Checked Then
            'setMediaPlayer = TextBoxOther.Text.ToLower
            'End If
            'If Not File.Exists(setMediaPlayer) Then
            'TextBoxOther.BackColor = Color.Red
            'MessageBox.Show("Your settings were NOT saved! Check your media player selection!", "- SiNet -", MessageBoxButtons.OK, MessageBoxIcon.Error)
            'bye = 0
            'End If

            ''Required settings check
            If Not setCountry.Length > 0 Then
                MessageBox.Show("You must select a country.", "- SiNet Error -", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If
            If Not TextBoxUsername.TextLength > 0 Then
                MessageBox.Show("You must enter a username.", "- SiNet Error -", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If
            If Not (TextBoxPassword.TextLength > 0) And Not (TextBoxPassword.Text <> "<ENCRYPTED>") Then
                MessageBox.Show("You must enter a password.", "- SiNet Error -", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If
            ''Make sure all tests passed
            ''Set the settings entered
            If Not setCountry = My.Settings.sinetCountry Then
                My.Settings.sinetCountry = setCountry
            End If
            My.Settings.sinetUser = TextBoxUsername.Text
            My.Settings.sinetPass = GenerateHash(TextBoxPassword.Text)
            ''This sets the notification window timeout
            If CheckBoxNoteWindow.Checked Then
                My.Settings.sinetNotification = ComboBoxPopupSec.SelectedIndex
            Else
                My.Settings.sinetNotification = 0
            End If
            ''This sets the proxy information (if any)
            My.Settings.proxyServer = TextBoxProxyServer.Text
            My.Settings.proxyUser = TextBoxProxyUser.Text
            If TextBoxProxyPass.TextLength > 0 And Not TextBoxProxyPass.Text.Equals("<ENCRYPTED>") Then
                My.Settings.proxyPass = GenerateHash(TextBoxProxyPass.Text)
            End If
            ''This sets the rest of the form
            My.Settings.sinetPidginStatus = CheckBoxPidgin.CheckState
            My.Settings.sinetLogSong = CheckBoxStoreSongInfo.CheckState
            My.Settings.sinetTaskBarOn = CheckBoxTaskBar.CheckState
            My.Settings.sinetTaskTrayOn = CheckBoxTaskTray.CheckState
            My.Settings.sinetSortByName = CheckBoxSortByName.CheckState
            My.Settings.sinetGuest = CheckBoxGuest.CheckState
            My.Settings.sinetHighBitrate = CheckBoxBitRate.CheckState
            ''SiNet Siren Account info
            My.Settings.sinetSirenOnConnect = CheckBoxSirenConnect.CheckState
            ''Handle the SiNet Siren account
            My.Settings.sinetSirenUser = TextBoxSirenUser.Text
            My.Settings.sinetSirenPass = TextBoxSirenPass.Text
            ''Doozer - "Bye-Bye" Oh, and save settings
            My.Settings.Save()

            ''Enable the log-in button, maybe
            If Not Main.ComboStreamBox.Enabled Then
                Main.LoginToolStripMenuItem.Enabled = True
            End If
            ''Reset the public variable getCountryURL
            If My.Settings.sinetCountry = "us" Then
                Main.theCountryURL = My.Settings.hostUS
                Main.setCountryURL()
            ElseIf My.Settings.sinetCountry = "ca" Then
                Main.theCountryURL = My.Settings.hostCA
                Main.setCountryURL()
            End If
            ''Re-sort the stream combo box, maybe
            If (Not My.Settings.sinetSortByName = setStatFormat) And Main.ComboStreamBox.Enabled Then
                writeLog(4, "Reloading stream selection box...", True)
                getURL(Main.theCountryURL, "getstreams")
            End If
            ''Connect to the SirenServer, maybe
            If setSirenInfo And My.Settings.sinetSirenOnConnect And Not Main.jConnected Then
                ''Let's connect
                Main.JabberClient1.Connect()
                Main.JabberClient1.User = My.Settings.sinetSirenUser
                Main.JabberClient1.Password = My.Settings.sinetSirenPass
                Main.JabberClient1.Login()
            End If
            Me.Close()
        Catch ex As Exception
            MessageBox.Show("Could not save your settings, try restarting SiNet? Error: " & ex.Message, "- siNet -", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Application.Exit()
        End Try
    End Sub

    Private Sub CheckBoxNoteWindow_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxNoteWindow.CheckedChanged
        If CheckBoxNoteWindow.Checked Then
            ComboBoxPopupSec.Visible = True
            ComboBoxPopupSec.SelectedIndex = 5
            LabelSecInfo.Visible = True
        Else
            ComboBoxPopupSec.Visible = False
            LabelSecInfo.Visible = False
        End If
    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Try
            If SirenReg.ShowDialog() = Windows.Forms.DialogResult.OK Then
                setSirenInfo = True
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub RadioButtonProxyIE_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonProxyIE.CheckedChanged, RadioButtonProxyNone.CheckedChanged
        If RadioButtonProxyIE.Checked Then
            TextBoxProxyServer.Text = Nothing
            TextBoxProxyUser.Text = Nothing
            TextBoxProxyPass.Text = Nothing
        End If
    End Sub

End Class